<?php

declare(strict_types=1);
/**
 * This file is part of MineAdmin.
 *
 * @link     https://www.mineadmin.com
 * @document https://doc.mineadmin.com
 * @contact  root@imoi.cn
 * @license  https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
 */

namespace App\Http\Admin\Controller\Word;

use App\Http\Admin\Controller\AbstractController;
use App\Http\Admin\Middleware\PermissionMiddleware;
use App\Http\Admin\Request\Word\WordRequest;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Middleware\OperationMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use App\Schema\Word\WordSchema;
use App\Service\Word\WordService;
use Hyperf\Collection\Arr;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Request;
use Hyperf\Swagger\Annotation\Delete;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\Put;
use Mine\Access\Attribute\Permission;
use Mine\Swagger\Attributes\PageResponse;
use Mine\Swagger\Attributes\ResultResponse;
use OpenApi\Attributes\RequestBody;

#[HyperfServer(name: 'http')]
#[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
#[Middleware(middleware: PermissionMiddleware::class, priority: 99)]
#[Middleware(middleware: OperationMiddleware::class, priority: 98)]
final class WordController extends AbstractController
{
    public function __construct(
        private readonly WordService $service,
        private readonly CurrentUser $currentUser
    ) {}

    #[Get(
        path: '/admin/word',
        operationId: 'wordList',
        summary: '单词列表',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['单词管理']
    )]
    #[Permission(code: 'word:list')]
    #[PageResponse(instance: WordSchema::class)]
    public function pageList(Request $request): Result
    {
        return $this->success(
            $this->service->page(
                $request->all(),
                $this->getCurrentPage(),
                $this->getPageSize()
            )
        );
    }



    #[Put(
        path: '/admin/word/{id:\d+}',
        operationId: 'wordUpdate',
        summary: '更新单词',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['单词管理']
    )]
    #[Permission(code: 'word:update')]
    #[RequestBody(content: new JsonContent(ref: WordRequest::class, title: '更新单词'))]
    #[ResultResponse(new Result())]
    public function update(int $id, WordRequest $request): Result
    {
        $this->service->updateById($id, $request->validated());
        return $this->success();
    }


    #[Post(
        path: '/admin/word',
        operationId: 'wordCreate',
        summary: '创建单词',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['单词管理']
    )]
    #[Permission(code: 'word:create')]
    #[RequestBody(content: new JsonContent(ref: WordRequest::class, title: '创建单词'))]
    #[ResultResponse(new Result())]
    public function create(WordRequest $request): Result
    {
        $this->service->create($request->validated());
        return $this->success();
    }

    #[Delete(
        path: '/admin/word',
        operationId: 'wordDelete',
        summary: '删除单词',
        security: [['Bearer' => [], 'ApiKey' => []]],
        tags: ['单词管理']
    )]
    #[Permission(code: 'word:delete')]
    #[ResultResponse(new Result())]
    public function delete(Request $request): Result
    {
        $this->service->deleteById($request->all(), false);
        return $this->success();
    }
}
